//
//  DangerTableVC.m
//  Tunnel
//
//  Created by wbb on 2025/6/30.
//

#import "DangerTableVC.h"

typedef void(^TextChangeBlock)(NSString * _Nullable text);

@interface DangerCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *detailLab;
@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (nonatomic, strong) NSMutableDictionary * dataDic;
@property (nonatomic, copy) TextChangeBlock textChangeBlock;
@end

@implementation DangerCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.tf addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)setDataDic:(NSMutableDictionary *)dataDic {
    _dataDic = dataDic;
    self.titleLab.text = dataDic[@"propertyName"];
    self.detailLab.text = dataDic[@"unit"];
    
}
- (void)textChange:(UITextField *)tf {
    self.dataDic[@"propertyValue"] = tf.text;
}
@end


@interface DangerTableVC ()<UITextFieldDelegate>
@property (nonatomic, strong) NSMutableArray * categoryInsertRequestList;

@end

@implementation DangerTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注册键盘通知以手动处理
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dangerList.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return  0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DangerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DangerCell" forIndexPath:indexPath];
    cell.dataDic = self.dangerList[indexPath.row];
    return cell;
}

- (void)keyboardWillShow {
    self.tableView.contentOffset = CGPointMake(0, 0);
}
- (void)keyboardWillHide {
    self.tableView.contentOffset = CGPointMake(0, 0);
}
@end
